<template>
    <div class="pressForm">
      <!-- element组件库的Dialog对话框默认可以通过点击 modal 关闭 Dialog，即点击空白处弹框可关闭。 -->
      <!-- :visible.sync 子组键改变父组件的值 控制子组件的显示-->
      <el-dialog title="新增出版社" :visible.sync="addDialogIsShow" :before-close="handleClose" :close-on-click-modal="false">
      <el-form :model="press" label-width="80px" :rules="addPressRules" ref="addPressForm">
        <el-form-item label="出版社标题" prop="prname">
          <el-col :span="16">
           <el-input v-model="press.prname" placeholder="请填入标题" clearable/>
          </el-col>
        </el-form-item>
        <el-form-item label="出版社联系方式" prop="prphone">
          <el-col :span="16">
            <el-input v-model="press.prphone" placeholder="请填入标题" clearable/>
          </el-col>
        </el-form-item>
        <el-form-item label="数量" prop="prnumber">
          <el-col :span="16">
            <el-input v-model="press.prnumber" placeholder="请填入数量" clearable/>
          </el-col>
        </el-form-item>
        <el-form-item label="账号" prop="prid">
          <el-col :span="16">
            <el-input v-model="press.prid" placeholder="请填入账号" clearable/>
          </el-col>
        </el-form-item>
        <el-form-item label="密码" prop="prpwd">
          <el-col :span="16">
            <el-input v-model="press.prpwd" placeholder="请填入密码" clearable/>
          </el-col>
        </el-form-item>
        <el-form-item label="详情" prop="prdescribe">
          <el-col :span="16">
            <el-input type="textarea" :rows="5" v-model="press.prdescribe" placeholder="请输入出版社详情" />
          </el-col>
        </el-form-item>
        <el-form-item label="邮箱" prop="premail">
          <el-col :span="16">
            <el-input type="textarea" :rows="5" v-model="press.premail" placeholder="请输入邮箱" />
          </el-col>
        </el-form-item>
      </el-form>
      <el-row>
         <el-col :span="12">
         <span>&nbsp;</span></el-col>
        <el-col :span="6">
           <span><el-button type="primary" @click="addPress('addPressForm')">添加</el-button></span>
        </el-col>
    </el-row>
      </el-dialog>
    </div>
</template>

<script>
  import api from "../../../../axios/api"
  import rules from "../../../../static/js/validator/rules.js"
    var initfavOption = [];
    export default {
        name: "addPress",
        data(){
          return {press:{},
              checkAll: false,
              isIndeterminate: true,
              addPressRules:{ prname:[{required:true,message:"名称不能为空",trigger:'blur'}],
                prphone:[{required:true,message:"联系方式不能为空",trigger:'blur'}],
                prnumber:[{required:true,message:"数量不能为空",trigger:'blur'}],
                prid:[{required:true,message:"账号不能为空",trigger:'blur'}],
                prpwd:[{required:true,message:"密码不能为空",trigger:'blur'}],
                prdescribe:[{required:true,message:"详情不能为空",trigger:'blur'}],
                premail:[{required:true,message:"邮箱不能为空",trigger:'blur'}],},
              addDialogIsShow:false
          }
        },
      methods: {
        init() {
          this.addDialogIsShow = true;
        },
        handleCheckAllChange(val) {
          this.press.favs = val ? initfavOption : [];
          this.isIndeterminate = false;
        },
        handleCheckedFavsChange(value) {
          let checkedCount = value.length;
          this.checkAll = checkedCount === this.favs.length;
          this.isIndeterminate = checkedCount > 0 && checkedCount < this.favs.length;
        },
        //关闭页面
        handleClose() {
          this.addDialogIsShow = false;
          //给父组件传参
          this.$emit("addDialogIsShow", this.addDialogIsShow);
        },
        addPress:function(addPressForm){
          this.$refs[addPressForm].validate(valid => {//此处refs对象和标签上的ref对应
            if (valid) {
              //如果通过验证 to do...
              console.log(this.press);
              alert(this.press.prname+"添加成功");
              this.setDataFromAxios();
              this.handleClose();

            } else {
              console.log('error submit!!')
              return false
            }
          });
        },
        //关闭页面
        handleClose() {
          this.addDialogIsShow = false;
          //给父组件传参

          this.$emit("addDialogIsShow", this.addDialogIsShow);
        },
        setDataFromAxios:function(){
         alert(this.$store.state.host);
          //为什么要封装一下方法 1.每个vue组件有自己的一些参数处理，简化调用本来这个setDataFromAxios这个方法属于api 重新封装就属于当前页面
          api.setDataFromAxios("/api/pressController/addPress",this.press,"post")
            .then(res=>{this.leagues = res.leagues});//.then方法 就是当服务器有返回的时候执行回调函数，对比jquery的ajax success:function（data）
        },
        updateView(e) {
          this.$forceUpdate();
        }

      },
      //监听
      watch: {
        addDialogIsShow(newValue, oldValue) {//配合:visible.sync监听变化
          this.addDialogIsShow = newValue;
        }
      }
    }
</script>
<style scoped lang="stylus">
 .pressForm
   margin 0 auto
   width 1000px
   text-align left
</style>
